/**
 * Huisou.com Inc.
 * Copyright (c) 2011-2012 All Rights Reserved.
 */

package com.chenzehe.zdmya.action;

import static com.chenzehe.java.core.Constants.PAGE_PATH_404;
import static com.chenzehe.java.core.Status.STATUS_NORMAL;
import static com.chenzehe.zdmya.Constants.SESSION_USER;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import com.chenzehe.java.core.freemarker.HtmlPathHelp;
import com.chenzehe.java.core.orm.Page;
import com.chenzehe.java.core.orm.PageRequest;
import com.chenzehe.java.core.utils.WebUtil;
import com.chenzehe.zdmya.domain.Area;
import com.chenzehe.zdmya.domain.Category;
import com.chenzehe.zdmya.domain.Collection;
import com.chenzehe.zdmya.domain.Content;
import com.chenzehe.zdmya.domain.ContentType;
import com.chenzehe.zdmya.domain.Coupon;
import com.chenzehe.zdmya.domain.Fx;
import com.chenzehe.zdmya.domain.Jy;
import com.chenzehe.zdmya.domain.Message;
import com.chenzehe.zdmya.domain.OperatePoint;
import com.chenzehe.zdmya.domain.User;
import com.chenzehe.zdmya.service.IAreaService;
import com.chenzehe.zdmya.service.ICategoryService;
import com.chenzehe.zdmya.service.IContentService;
import com.chenzehe.zdmya.service.IContentTypeService;
import com.chenzehe.zdmya.service.ICouponService;
import com.chenzehe.zdmya.service.IFxService;
import com.chenzehe.zdmya.service.IJyService;
import com.chenzehe.zdmya.service.IMessageService;
import com.chenzehe.zdmya.service.IUserService;

/**
 * @description
 * 
 * @author chenzehe
 * @email hljuczh@163.com
 * @create 2012-7-20 下午11:15:36
 */
@Controller
public class ContentAction extends BaseAction {
	@Resource
	IContentService contentService;
	@Resource
	IFxService fxService;
	@Resource
	ICouponService couponService;
	@Resource
	IJyService jyService;
	@Resource
	IMessageService messageService;
	@Resource
	ICategoryService categoryService;
	@Resource
	IContentTypeService typeService;
	@Resource
	IAreaService areaService;
	@Resource
	IUserService userService;

	@RequestMapping(value = "/page/index.html")
	public String index(HttpServletRequest request, ModelMap model) {
		int pageNo = WebUtil.getInt(request, "pageNo", 1);
		int pageSize = WebUtil.getInt(request, "pageSize", 15);
		PageRequest pageRequest = new PageRequest(pageNo, pageSize);
		pageRequest.setOrderBy("id");
		pageRequest.setOrderDir(PageRequest.Sort.DESC);
		Page<Content> allPageContent = contentService
				.queryPageContent(pageRequest);
		model.addAttribute("allPageContent", allPageContent);

		// 如果是第一页就查询置顶数据
		if (pageNo == 1) {
			List<Content> topPageContent = contentService.queryTopContent();
			model.addAttribute("topPageContent", topPageContent);
		}

		model.addAttribute("currentMenuIndex", "currentMenu");

		List<Fx> limitFxs = fxService.queryLimitFx(12);
		List<Coupon> limitCoupons = couponService.queryLimitCoupons(10);
		List<Jy> limitJys = jyService.queryLimitJy(5);
		model.addAttribute("limitFxs", limitFxs);
		model.addAttribute("limitCoupons", limitCoupons);
		model.addAttribute("limitJys", limitJys);

		return HtmlPathHelp.getHtmlPathFront("index");
	}

	@RequestMapping(value = "/category/index.html")
	public String categoryContent(HttpServletRequest request, ModelMap model) {
		String ename = WebUtil.getString(request, "ename");
		String parentEname = WebUtil.getString(request, "pename");
		if (StringUtils.isNotBlank(ename)) {
			model.addAttribute("categoryename", ename);
			Category category = categoryService.getByename(ename);
			if (category != null) {
				model.addAttribute("categoryname", category.getName());
			}
		}
		model.addAttribute("parentcategoryename", parentEname);
		if (StringUtils.isNotBlank(parentEname)) {
			Category parentCategory = categoryService.getByename(parentEname);
			if (parentCategory != null) {
				model.addAttribute("parentcategoryname",
						parentCategory.getName());
			}
		}
		int pageNo = WebUtil.getInt(request, "pageNo", 1);
		int pageSize = WebUtil.getInt(request, "pageSize", 2);
		PageRequest pageRequest = new PageRequest(pageNo, pageSize);
		pageRequest.setOrderBy("id");
		pageRequest.setOrderDir(PageRequest.Sort.DESC);
		Page<Content> categoryContent = contentService.queryByCategory(
				pageRequest, parentEname, ename);
		model.addAttribute("categoryContent", categoryContent);

		model.addAttribute("currentMenuType", "currentMenu");

		List<Fx> limitFxs = fxService.queryLimitFx(12);
		List<Coupon> limitCoupons = couponService.queryLimitCoupons(10);
		List<Jy> limitHaiTaoJys = jyService.queryLimitJyByArea(5,
				Jy.Area.HTJY.getArea());
		model.addAttribute("limitFxs", limitFxs);
		model.addAttribute("limitCoupons", limitCoupons);
		model.addAttribute("limitHaiTaoJys", limitHaiTaoJys);

		return HtmlPathHelp.getHtmlPathFront("categorycontent");
	}

	@RequestMapping(value = "/type/index.html")
	public String typeContent(HttpServletRequest request, ModelMap model) {
		String typeename = WebUtil.getString(request, "pename");
		if (StringUtils.isNotBlank(typeename)) {
			model.addAttribute("typeename", typeename);
			ContentType type = typeService.getTypeByEname(typeename);
			if (type != null) {
				model.addAttribute("typename", type.getName());
			}
		}
		int pageNo = WebUtil.getInt(request, "pageNo", 1);
		int pageSize = WebUtil.getInt(request, "pageSize", 2);
		PageRequest pageRequest = new PageRequest(pageNo, pageSize);
		pageRequest.setOrderBy("id");
		pageRequest.setOrderDir(PageRequest.Sort.DESC);
		Page<Content> typeContent = contentService.queryByType(pageRequest,
				typeename);
		model.addAttribute("typecontent", typeContent);

		model.addAttribute("currentMenuType", "currentMenu");

		List<Fx> limitFxs = fxService.queryLimitFx(12);
		List<Coupon> limitCoupons = couponService.queryLimitCoupons(10);
		List<Jy> limitHaiTaoJys = jyService.queryLimitJy(5);
		model.addAttribute("limitFxs", limitFxs);
		model.addAttribute("limitCoupons", limitCoupons);
		model.addAttribute("limitHaiTaoJys", limitHaiTaoJys);

		return HtmlPathHelp.getHtmlPathFront("typecontent");
	}

	@RequestMapping(value = "/area/index.html")
	public String areaContent(HttpServletRequest request, ModelMap model) {
		String typeename = WebUtil.getString(request, "pename");
		if (StringUtils.isNotBlank(typeename)) {
			model.addAttribute("areaename", typeename);
			Area area = areaService.getAreaByEname(typeename);
			if (area != null) {
				model.addAttribute("areaname", area.getName());
			}
		}
		int pageNo = WebUtil.getInt(request, "pageNo", 1);
		int pageSize = WebUtil.getInt(request, "pageSize", 2);
		PageRequest pageRequest = new PageRequest(pageNo, pageSize);
		pageRequest.setOrderBy("id");
		pageRequest.setOrderDir(PageRequest.Sort.DESC);
		Page<Content> areaContent = contentService.queryByArea(pageRequest,
				typeename);
		model.addAttribute("areacontent", areaContent);

		model.addAttribute("currentMenuType", "currentMenu");

		List<Fx> limitFxs = fxService.queryLimitFx(12);
		List<Coupon> limitCoupons = couponService.queryLimitCoupons(10);
		List<Jy> limitJys = jyService.queryLimitJy(5);
		model.addAttribute("limitFxs", limitFxs);
		model.addAttribute("limitCoupons", limitCoupons);
		model.addAttribute("limitJys", limitJys);

		return HtmlPathHelp.getHtmlPathFront("areacontent");
	}

	@RequestMapping(value = "/content/detail.html")
	public String contentShow(HttpServletRequest request, ModelMap model) {
		int id = WebUtil.getInt(request, "id");
		Content content = contentService.getById(id);
		if (content == null || content.getStatus() != STATUS_NORMAL.getState()) {
			return PAGE_PATH_404;
		}
		content.setPreContent(contentService.getPreContentById(id));
		content.setNextContent(contentService.getNextContentById(id));
		model.addAttribute("content", content);
		model.addAttribute("collectionType",
				Collection.CollectionType.CONTENT.type());
		model.addAttribute("categoryename", content.getCategoryEname());
		model.addAttribute("categoryname", content.getCategoryName());
		model.addAttribute("parentcategoryename",
				content.getCategoryParentEname());
		model.addAttribute("parentcategoryname",
				content.getCategoryParentName());
		model.addAttribute("typeename", content.getTypeEname());
		model.addAttribute("typename", content.getTypeName());
		model.addAttribute("areaename", content.getAreaEname());
		model.addAttribute("areaname", content.getAreaName());

		List<Content> listContents = contentService
				.queryLimitContentByCategoryEname(5, content.getCategoryEname());
		model.addAttribute("listContents", listContents);

		int pageNo = WebUtil.getInt(request, "pageNo", 1);
		int pageSize = WebUtil.getInt(request, "pageSize", 5);
		PageRequest pageRequest = new PageRequest(pageNo, pageSize);
		pageRequest.setOrderBy("id");
		pageRequest.setOrderDir(PageRequest.Sort.ASC);
		Page<Message> messages = messageService.queryByContentId(pageRequest,
				Message.MessageType.CONTENT.type(), id);
		model.addAttribute("messages", messages);

		User user = (User) WebUtils.getSessionAttribute(request, SESSION_USER);
		model.addAttribute("user", user);
		
		model.addAttribute("currentMenuType", "currentMenu");

		List<Fx> limitFxs = fxService.queryLimitFx(12);
		List<Coupon> limitCoupons = couponService.queryLimitCoupons(10);
		List<Jy> limitJys = jyService.queryLimitJy(5);
		model.addAttribute("limitFxs", limitFxs);
		model.addAttribute("limitCoupons", limitCoupons);
		model.addAttribute("limitJys", limitJys);

		return HtmlPathHelp.getHtmlPathFront("detail");
	}

	@RequestMapping(value = "/content/pingfen.html")
	@ResponseBody
	public String pingfen(HttpServletRequest request) {
		int commentId = WebUtil.getInt(request, "pid", 0);
		int rate = WebUtil.getInt(request, "rate", 0);
		if (commentId == 0 && rate == 0) {
			return "{\"status\":\"no\"}";
		}
		contentService.updatePingFen(commentId, rate);
		User user = (User) WebUtils.getSessionAttribute(request, SESSION_USER);
		if (user != null) {
			userService.operatePoint(user.getId(), OperatePoint.LIKE);
		}
		return "{\"status\":\"ok\"}";
	}

	@RequestMapping(value = "/content/go.html")
	public String mallgo(HttpServletRequest request) {
		int id = WebUtil.getInt(request, "id");
		String goUrl = contentService.getGoUrl(id);
		if (StringUtils.isNotBlank(goUrl)) {
			return "redirect:" + goUrl;
		}
		return "redirect:/";
	}

}
